const Registry = require('winreg');
let copyToolsList = require('./data/copyToolsList');
let batToolsList = require('./data/batToolsList');
let fileToolsList = require('./data/fileToolsList');
let dirToolsList = require('./data/dirToolsList');
const methodToPromise = function (host, method) {
  return new Promise((resolve, reject) => {
    if (!host || !method) {reject('宿主名和方法名不能为空')}
    host[method](function (err) {
      if (err) {reject(err)}
      else {resolve(...arguments)}
    });
  })
}
let ret_bat = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\Directory\\Background\\shell\\批处理便捷工具'
  });
  await methodToPromise(regKey, 'destroy');
  batToolsList.forEach(async (v) => {
    let regKey = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    await methodToPromise(regKey, 'destroy');
  });
  console.log('[卸载完成] 批处理便捷工具');
})();
let ret_copy = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\Directory\\Background\\shell\\前端便捷工具'
  });
  await methodToPromise(regKey, 'destroy');
  copyToolsList.forEach(async (v) => {
    let regKey = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    await methodToPromise(regKey, 'destroy');
  });
  console.log('[卸载完成] 文件夹空白处·前端便捷工具');
})();
let ret_file = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\*\\shell\\XTools'
  });
  await methodToPromise(regKey, 'destroy');
  fileToolsList.forEach(async (v) => {
    let regKey = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    await methodToPromise(regKey, 'destroy');
  });
  console.log('[卸载完成] 文件·前端便捷工具');
})();
let ret_dir = (async function () {
  let regKey = new Registry({
    hive: Registry.HKCR,
    key: '\\Directory\\shell\\前端便捷工具'
  });
  await methodToPromise(regKey, 'destroy');
  dirToolsList.forEach(async (v) => {
    let regKey = new Registry({
      hive: Registry.HKLM,
      key: `\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\${v.value}`
    });
    await methodToPromise(regKey, 'destroy');
  });
  console.log('[卸载完成] 文件夹·前端便捷工具');
})();
